rm(list=ls())
load("Data/study_1.RData")
library(ggpubr)
library(GGally)

# Make Figure 1A of paper ----------
subset <- data.frame(data$range_party_sympathy,
                     data$absolute_outparty_sympathy,
                     data$Distant_Inparty_favoritism,
                     data$Distant_Outparty_disfavoritism)
colnames(subset) <- c("inparty\ndirect", "outparty\ndirect", "inparty\nconceptual", "outparty\nconceptual")

cors <- ggpairs(subset, diag="blank", lower=list(continuous = wrap("points", alpha = 0.3, size=0.1), 
                                            combo = wrap("dot", alpha = 0.4, size=0.2)))
cors <- cors +   theme(axis.text.x = element_text(angle = 90, hjust = 1))

ggsave(cors, file="Tables and Figures/corplot.jpg", dpi=1000)

# Make Figure 1B of paper ---------
load("Data/simas_clifford_kirkland_study1_replication.RData")

#subset2 <- data.frame(subset, table$affectpol, table$outfav)
value <- stack(subset)
value$study <- "GSR"
table$infav <- table$affectpol + table$outfav
table$infav <- (table$infav-1) * 10/6
table$outfav <- (table$outfav-1) * 10/6
table$affectpol <- table$infav - table$outfav

temp <- c(table$affectpol, table$outfav)
value2 <- data.frame(temp, rep(c("inparty SCK", "outparty SCK"), each=1000))
value2 <- value2[-which(table$affectpol<0),] # drop people who favor outparty more than inparty
colnames(value2) <- c("values", "ind")
value2$study <- "SCK"
value <- rbind(value, value2)
value$inparty <- ifelse(substr(value$ind,1,2)=="in","inparty","outparty")

cbbPalette <- c("#000000", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")

densities <- ggplot(value, aes(x=values, group=ind, fill=ind)) +
  geom_density(alpha=.75) + facet_grid(inparty~., scales="free") +
  scale_fill_manual(values=cbbPalette) +
  theme(legend.title=element_blank(),
        legend.position="bottom")


ggsave(densities, file="Tables and Figures/densityplot.jpg", dpi=1000)
